/* eslint-disable require-jsdoc */
/* eslint-disable valid-jsdoc */
// 处理路由工具函数
import layout from "@/layout";
import { isExternal } from "@/util/validate";
// const _import = require("./config/_import_" + process.env.NODE_ENV); // 获取组件的方法
// console.log(require("@/views/dashboard/personal/home")["default"]);
// console.log(_import("src/dashboard/personal/home"));
function devRequire(path) {
  return require(`@/views${path}`)["default"];
}

/**
 * 后台查询的菜单数据拼装成路由格式的数据
 * @param routes
 */
export function generaMenu(routes, data) {
  data.forEach(item => {
    // alert(JSON.stringify(item))
    let component =
      item.path === ""
        ? layout
        : !isExternal(item.path)
        ? devRequire(item.path)
        : item.path;
    const menu = {
      ...item,
      path: item.path === "" ? "" : item.path,
      component,
      // hidden: true,
      children: [],
      name: item.name,
      meta: { title: item.name, id: item.id }
    };
    if (item.children) {
      generaMenu(menu.children, item.children);
    }
    routes.push(menu);
  });
  return routes;
}

export function filterAsyncRouter(asyncRouterMap) {
  // 遍历后台传来的路由字符串，转换为组件对象
  const accessedRouters = asyncRouterMap.filter(route => {
    if (route.path === "") {
      // Layout组件特殊处理
      route.component = layout;
    } else {
      route.component =
        route.path === ""
          ? layout
          : !isExternal(route.path)
          ? devRequire(route.path)
          : layout;
    }
    if (route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children);
    }
    return true;
  });

  return accessedRouters;
}
